﻿using System.Collections.Generic;
using System.Text;
using Camdug.Web.Controllers;
using Camdug.Web.Services;
using NUnit.Framework;
using Specification = NUnit.Framework.TestAttribute;

namespace Camdug.Web.Tests
{
    [TestFixture]
    public class When_registering_as_a_member_of_the_site
    {
        private RegisterController registerController;

        [SetUp]
        public void SetUp()
        {
            IRegisteredMembersRepository mockedRegisteredMembersRepository = new MockedRegisteredMembersRepository();
            registerController = new RegisterController(mockedRegisteredMembersRepository);
        }

        [Specification]
        public void Should_go_to_a_register_page()
        {
            var view = registerController.Index();
            Assert.That(view, Is.Not.Null);
        }

        [Specification]
        public void Should_supply_a_username_and_password_to_register_with()
        {
            var view = registerController.Register("UserName", "password");
            Assert.That(view, Is.EqualTo(true));
        }

        [Specification]
        public void Should_not_be_able_to_supply_a_null_password()
        {
            string passwordNotSupplied = null;
            var wasRegistered = registerController.Register("Username", passwordNotSupplied);
            Assert.That(wasRegistered, Is.False);
        }

        [Specification]
        public void Should_not_be_able_to_supply_an_empty_password()
        {
            var emptyPassword = string.Empty;
            var wasRegistered = registerController.Register("Username", emptyPassword);
            Assert.That(wasRegistered, Is.False);
        }

        [Specification]
        public void Should_only_register_if_username_has_not_previously_been_registered()
        {
            var sameUserName = "SameUser";
            var firstResult = registerController.Register(sameUserName, "withpassword");
            var secondRegistrationOfSameUserResult = registerController.Register(sameUserName, "withanotherpassword");
            Assert.That(secondRegistrationOfSameUserResult, Is.False);
        }

    
        public class MockedRegisteredMembersRepository : IRegisteredMembersRepository
        {
            private IList<string> users = new List<string>();

            public bool SaveUser(string username, string password)
            {
                if(!IsUserExist(username))
                {
                    users.Add(username);
                    return true;
                }
                else
                {
                    return false;
                }

            }

            public bool IsUserExist(string username)
            {
                return users.Contains(username);
            }
        }
    }

}
